package com.struts.interceptor;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * 用户登录权限验证拦截器
 * @author LC
 *
 */
public class LoginPrivilegeInterceptor implements Interceptor{

	public void destroy() {
		System.out.println("销毁LoginInterceptor自定义拦截器");
	}

	public void init() {
		System.out.println("初始化LoginInterceptor自定义拦截器");
	}
	
	/**
	 * 拦截用户登录url请求，验证用户名和密码是否正确
	 */
	public String intercept(ActionInvocation invocation) throws Exception {
		//获取页面请求参数
		String username = ServletActionContext.getRequest().getParameter("username") ;
		String password = ServletActionContext.getRequest().getParameter("password") ;
		if("admin".equals(username) && "admin".equals(password)) {
			System.out.println("用户名和密码输入正确，放行url请求");
			String result = invocation.invoke() ;
			System.out.println("用户请求登录动作方法执行完成之后执行...");
			return result ;
		} else {
			System.out.println("用户名或密码错误...");
			ActionContext.getContext().put("error", "用户名或密码错误...") ;
			return "login" ;
		}
	}
	

}
